var {
  randomUtils,
  textUtils,
  timeUtils,
  clickUtils,
  ocrUtils,
  fmtUtils,
  pauseUtils,
  logUtils,
  clickContent,
  appUtils,
  configUtils,
} = require ('../../utils/index');
var adCommon = require ('../adCommon');
var manual = configUtils.getConfig ('ad', 'manual', false);
module.exports = {
  watchAd (span, handleNextAd) {
    this.appName = '今日头条';
    this.span = span;
    this.handleNextAd = handleNextAd;
    var startTime = Date.now ();
    var count = 0;
    var hasCheckInAd = false;
    var hasCheckDetail = false;
    adCommon.adCount++;
    logUtils.info ('第', adCommon.adCount, '/', adCommon.maxAdCount, '个广告');
    while (true) {
      count++;
      sleep (2 * 1000);
      var hasExec = timeUtils.hasExec (startTime);
      if (count % 5 == 0) {
        logUtils.info ('已经观看', hasExec, '/', this.span);
      }
      if (!hasCheckInAd) {
        var checkExec = hasCheckDetail ? 50 : 30;
        if (hasExec > checkExec) {
          hasCheckInAd = true;
          if (!this.inAd ()) {
            logUtils.info ('超过30s，不在广告页，执行一次back');
            back ();
            sleep (randomUtils.getRandom (2, 4, 1000));
          }
        }
      }
      if (!hasCheckDetail) {
        if (this.checkDetail ()) {
          hasCheckDetail = true;
          this.span = this.span + 45;
        }
      }

      if (count >= 4 && this.isOver ()) {
        sleep (randomUtils.getRandom (2, 10, 1000));
        this.stopAd ();
        break;
      }
      if (hasExec > this.span) {
        if (count % 5 != 0) {
          logUtils.info ('已经观看', hasExec, '/', this.span, '将执行关闭广告');
        }
        this.stopAd ();
        break;
      }
    }
  },
  stopAd () {
    for (var index = 0; index < 2; index++) {
      back ();
      sleep (randomUtils.getRandom (1.5, 2, 1000));
      var exitContents = [
        //prettier-disable
        '坚持退出',
      ];
      var hasNextContents = [
        //prettier-disable
        '看视频再得',
      ];
      if (adCommon.adCount >= adCommon.maxAdCount) {
        var TextUI = textUtils.existsOneTextStartsWith (exitContents);
        if (TextUI) {
          clickUtils.clickTextUI (TextUI);
          sleep (randomUtils.getRandom (1.5, 2, 1000));
          return;
        }
      } else {
        var TextUI = textUtils.existsOneTextStartsWith (hasNextContents);
        if (TextUI) {
          var text$ = TextUI.text ();
          var goldNumber = fmtUtils.getNumber (text$);
          logUtils.info ('发现ad:', text$, goldNumber);
          if (goldNumber > 500) {
            clickUtils.clickTextUI (TextUI);
            sleep (randomUtils.getRandom (1.5, 2, 1000));
            this.handleNextAd && this.handleNextAd ();
            return;
          }
          if (manual) {
            if (goldNumber < 500 && goldNumber > 100) {
              pauseUtils.pause ('广告金币小于500，需要人工干预');
              pauseUtils.pause ('是否已经开始下一个广告？');
              sleep (randomUtils.getRandom (1, 5, 2, 1000));
              this.handleNextAd && this.handleNextAd ();
              return;
            }
          }
          var TextUI = textUtils.existsOneTextStartsWith (exitContents);
          if (TextUI) {
            clickUtils.clickTextUI (TextUI);
            sleep (randomUtils.getRandom (1.5, 2, 1000));
            return;
          }
        }
      }
      if (!this.inAd ()) {
        logUtils.info ('没有广告，退出');
        return;
      }
    }
  },
  inAd () {
    var contents = [
      //prettier-disable
      '领取成功',
      '广告',
      '秒后可领奖励',
    ];
    var TextUI = textUtils.existsOneTextStartsWith (contents);
    if (TextUI) {
      return TextUI;
    }
    var contents = [
      //prettier-disable
      '领取成功',
      '秒后可领奖励',
    ];
    var TextOCR = ocrUtils.captureExistsOneText (contents);
    if (TextOCR) {
      return TextOCR;
    }
    return false;
  },
  isOver () {
    var contents = [
      //prettier-disable
      '领取成功',
    ];
    var TextOCR = ocrUtils.captureExistsOneText (contents);
    if (TextOCR) {
      return true;
    }
    sleep (10 * 1000);
    var TextUI = textUtils.existsOneTextStartsWith (contents);
    if (TextUI) {
      return true;
    }
  },
  checkDetail () {
    var content = '点击视频';
    if (clickContent (content)) {
      sleep (randomUtils.getRandom (2, 3, 1000));
      if (this.checkOpen ()) {
        back ();
        sleep (randomUtils.getRandom (2, 3, 1000));
      } else {
        sleep (randomUtils.getRandom (15, 30, 1000));
      }
      back ();
      sleep (randomUtils.getRandom (2, 3, 1000));
      return true;
    }
    if (this.checkOpen ()) {
      return true;
    }
    return false;
  },
  checkOpen () {
    var content = '立即打开';
    if (clickContent (content)) {
      sleep (randomUtils.getRandom (3, 5, 1000));
      var content = '允许';
      if (clickContent (content)) {
        sleep (randomUtils.getRandom (3, 5, 1000));
      }
      sleep (randomUtils.getRandom (10, 20, 1000));
      appUtils.open (this.appName);
      return true;
    }
    return false;
  },
};
